<?php

    /**
     * @name 语言包类
     * @copyright tcsos inc
     * @version cooperation v1.0
     */
    class lang extends cooperation {

        //语言包变量
        private $lang_ary;

        /**
         * 构造函数
         * 
         * @return void
         */
        public function __construct() {
            $this->load_lang();
        }

        /**
         * 析构函数
         * 
         * @return void
         */
        public function __destruct() {

        }

        /**
         * 加载语言包
         * 
         * @return void
         */
        private function load_lang() {
            $config = $this->_config();
            $langpath = $this->_root() . '/source/lang.php';
            if($config['runtime']['cache']) {
                $langpath = $this->get_datapath('cache/runtime/lang');
                if(!is_file($langpath)) {
                    $zh_cn = file_get_contents($this->_root() . '/source/lang.php');
                    $zh_cn = $this->strip_whitespace($zh_cn);
                    $this->_file()->write_content($zh_cn, $langpath);                
                }            
            }

            //数据配置
            $this->lang_ary = require_once $langpath;            
        }    

        /**
         * 获取语言包信息
         * 
         * @param string $msg   信息
         * @param array $ary    替换的信息
         * @return string
         */
        public function get_msg($lang, $ary = '') {            
            $lang_msg = $this->lang_ary[$lang];
            if(!is_array($ary)) {
                return $lang_msg;
            }

            $replaces = $searchs = array();
            foreach($ary as $k => $v) {
                $searchs[] = '{' . $k . '}';
                $replaces[] = $v;
            }

            return str_replace($searchs, $replaces, $lang_msg);
        }  
    }
    $_lang = new lang();
?>
